﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using Nox.Amuse.Helpers;
using System.Windows;

namespace Nox.Amuse.Model
{
    [XmlRoot("MainWindowSettings")]
    public class MainWindowSettingsModel
    {
        /// <summary>
        /// The file name for this class to be saved under when serialized
        /// </summary>
        const string FileName="WindowSettings.xml";
        /// <summary>
        /// The height of the window on screen
        /// </summary>
        public double WindowHeight { get; set; }
        /// <summary>
        /// The width of the window on screen
        /// </summary>
        public double WindowWidth { get; set; }
        /// <summary>
        /// The left co-ordinate of the window on screen
        /// </summary>
        public double WindowLeft { get; set; }
        /// <summary>
        /// The right co-ordinate of the window on screen
        /// </summary>
        public double WindowTop { get; set; }
        /// <summary>
        /// The state of the window
        /// </summary>
        public WindowState WindowState { get; set; }
        
        /// <summary>
        /// Resets Window Sizes to default (does not notify)
        /// </summary>
        public void SetWindowDefaults()
        {
            double Nine = 9;
            double Ten = 10;
            double Twenty = 20;
            double ScreenX = SystemParameters.WorkArea.Width;
            double ScreenY = SystemParameters.WorkArea.Height;
            WindowWidth = ((ScreenX * Nine) / Ten);
            WindowHeight = ((ScreenY * Nine) / Ten);
            WindowLeft = (ScreenX / Twenty);
            WindowTop = (ScreenY / Twenty);
        }
        
        // Constructor
        private MainWindowSettingsModel()
        {
            App.Messenger.Register(Messages.Exiting, () => Save());
        }

        /// <summary>
        /// Attempts to Load settings from the disk and push them in to a new instance. If file is not found, loads defaults.
        /// </summary>
        /// <returns>A new settings model</returns>
        public static MainWindowSettingsModel Load()
        {
            var serializer = new XmlSerializer(typeof(MainWindowSettingsModel));
            var textReader = FileHelper.StartReadingTextFile(FileName);
            if (textReader != null)
            {
                try
                {
                    return (MainWindowSettingsModel)serializer.Deserialize(textReader);
                }
                catch
                {
                    return CreateFresh();
                }
                finally
                {
                    textReader.Close();
                }
            }
            else
                return CreateFresh();
        }
        
        /// <summary>
        /// Creates a new model with default settings
        /// </summary>
        /// <returns>A new settings model with default values</returns>
        private static MainWindowSettingsModel CreateFresh()
        {
            var newData = new MainWindowSettingsModel();
            newData.SetWindowDefaults();
            return newData;
        }

        /// <summary>
        /// Saves the data
        /// </summary>
        private void Save()
        {
            var serializer = new XmlSerializer(typeof(MainWindowSettingsModel));
            var writer = FileHelper.StartWritingTextFile(FileName);
            serializer.Serialize(writer, this);
            writer.Close();
        }

    }
}
